package com.toocms.jinjuhang.ui.live_streaming

import android.app.Application
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.data.index.LiveBannerData
import com.toocms.jinjuhang.data.index.LiveListData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.base.MultiItemViewModel
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.event.SingleLiveEvent

class LiveStreamingModel(application: Application) : BaseViewModel<BaseModel>(application) {
    private var p = 1

    val items = ObservableArrayList<MultiItemViewModel<*>>()
    val itemBinding = ItemBinding.of<MultiItemViewModel<*>> { binding, _, item ->
        when (item.itemType) {
            Constants.RecyclerViewItemType.ITEM_TYPE_ZERO ->
                binding.set(BR.liveStreamingBannerItemModel, R.layout.item_live_streaming_banner)
            Constants.RecyclerViewItemType.ITEM_TYPE_ONE ->
                binding.set(BR.liveStreamingItemModel, R.layout.item_live_streaming)
        }
    }

    val stopRefreshOrLoadSingleLiveEvent = SingleLiveEvent<Unit>()

    val refreshBindingCommand = BindingCommand<BindingAction>(BindingAction {
        refresh(false)
    })
    val loadBindingCommand = BindingCommand<BindingAction>(BindingAction {
        load()
    })

    init {
        refresh(true)
    }

    private fun refresh(isShowLoading: Boolean = false) {
        liveBanner(isShowLoading)
    }

    private fun load() {
        p++
        liveList(p)
    }


    private fun liveBanner(isShowLoading: Boolean = false) {
        items.clear()
        requestData(
            Urls.Index.LIVE_BANNER,
            resultClass = LiveBannerData::class.java,
            isShowLoading = isShowLoading,
            result = {
                if (!it.list.isNullOrEmpty()) {
                    items += LiveStreamingBannerItemModel(this, it.list)
                }
            },
            finish = {
                p = 1
                liveList(p)
            })
    }


    private fun liveList(p: Int, isShowLoading: Boolean = false) {
        val params = mutableMapOf<String, String?>()
        params["p"] = p.toString()
        requestData(
            Urls.Index.LIVE_LIST,
            params,
            resultClass = LiveListData::class.java,
            isShowLoading = isShowLoading,
            result = {
//                if (1 == p) items.clear()
                it.result?.room_info?.forEach {
                    items += LiveStreamingItemModel(this, ObservableField(it))
                }
            },
            finish = {
                stopRefreshOrLoadSingleLiveEvent.call()
            })
    }

}